package com.runa.rccprotocol.dto.kute;

import com.runa.protocol.util.ByteUtil;

/**
 * 2024/11/20 15:08
 * zhangYB
 */
public class KtValveRequestData {


    public byte[] readKtValve(String serial) {
        if (serial.length() % 2 == 1) {
            serial = "0" + serial;
        }
        byte type = 0x00;
        byte[] bytes = ByteUtil.hexStringToByte(serial);
        if (bytes.length == 6) {
            type = bytes[0];
            serial = serial.substring(2);
        }
        return readKtValve(serial, type);
    }

    public byte[] readKtValve(String serial, byte type) {
        byte[] requestData = new byte[16];
        int index = 0;
        requestData[index++] = 0x68;
        requestData[index++] = type;
        index += 2;
        byte[] bytes = ByteUtil.hexStringToByte(serial);
        for (int i = 0; i < bytes.length; i++) {
            requestData[index+i] = bytes[i];
        }
        index += 5;
        requestData[index++] = 0x03;
        requestData[index++] = 0x03;
        requestData[index++] = (byte)0x90;
        requestData[index++] = 0x1f;
        requestData[index++] = 0x00;
        requestData[index++] = ByteUtil.sum(requestData, 0, index);
        requestData[index++] = 0x16;
        return requestData;
    }

    public byte[] setKtValveOpen(String serial, Integer opening, boolean ifLock) {
        if (serial.length() % 2 == 1) {
            serial = "0" + serial;
        }
        byte type = 0x00;
        byte[] bytes = ByteUtil.hexStringToByte(serial);
        if (bytes.length == 6) {
            type = bytes[0];
            serial = serial.substring(2);
        }
        return setKtValveOpen(serial, type, opening, ifLock);
    }

    public static byte[] setKtValveOpen(String serial, byte type, Integer opening, boolean ifLock) {
        byte[] requestData = new byte[100];
        int index = 0;
        requestData[index++] = (byte)0xFE;
        requestData[index++] = (byte)0xFE;
        requestData[index++] = (byte)0xFE;
        int begin = index;
        requestData[index++] = 0x68;
        requestData[index++] = type;
        index += 2;
        byte[] bytes = ByteUtil.hexStringToByte(serial);
        for (int i = 0; i < bytes.length; i++) {
            requestData[index+i] = bytes[i];
        }
        index += 5;
        requestData[index++] = 0x2A;
        requestData[index++] = 0x04;
        requestData[index++] = (byte)0xA0;
        requestData[index++] = 0x17;
        requestData[index++] = 0x00;
        requestData[index++] = (byte)(opening & 0xff);
        index += 8;
        requestData[index++] = ByteUtil.sum(requestData, begin, index);
        requestData[index++] = 0x16;
        byte[] data = new byte[index];
        System.arraycopy(requestData, 0, data, 0, index);
        return data;
    }

    public static void main(String[] args) {
        KtValveRequestData requestData =  new KtValveRequestData();
        byte[] bytes = requestData.readKtValve("100124070696");
        System.out.println(ByteUtil.byteToHexString(bytes));
        bytes = requestData.setKtValveOpen("100124070696", 100, false);
        System.out.println(ByteUtil.byteToHexString(bytes));
    }

}
